const path = require("path")
const multer = require('@koa/multer')
const AVATAR_PATH = '../../upload';
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
      cb(null, path.resolve(__dirname ,AVATAR_PATH))
  },
  filename: function (req, file, cb) {
      let type = file.originalname.split('.')[1]
      cb(null, `${file.fieldname}-${Date.now().toString(16)}.${type}`)
  }
})

const limits = {
  fields: 10,//非文件字段的数量
  fileSize: 500 * 1024,//文件大小 单位 b
  files: 1//文件数量
}

const avatarUpload = multer(storage, limits)

// 处理用户头像
const avatarHandler = avatarUpload.single("file")

module.exports = {
  avatarHandler
}